<template>
  <el-dropdown trigger="click" placement="bottom-start" @command="handleAudit">
    <el-button>
      审 核<svg t="1731051363182" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7443" width="20" height="20"><path d="M302.016 367.232a32 32 0 0 0-6.976 40.32l3.328 4.736 214.848 252.16 212.48-252.288a32 32 0 0 0-44.8-45.312l-4.096 4.096-163.904 194.56-165.76-194.688a32 32 0 0 0-40.448-6.976l-4.672 3.392z" p-id="7444" fill="currentColor"></path></svg>
    </el-button>
    <template #dropdown>
      <el-dropdown-menu v-matomo>
        <el-dropdown-item v-for="(d, i) in list" :key="i" :command="d.value">{{ d.label
        }}</el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="item.label" v-model="visible" align-center width="800px" @close="handleClose">
    <el-form>
      <el-form-item label="审核">
        <el-radio-group v-model="form.audit_result" class="ml-4">
          <el-radio :value="1">通过</el-radio>
          <el-radio :value="2">驳回</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="驳回原因">
        <el-input v-model="form.audit_remark" type="textarea" :autosize="{ minRows: 5 }"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from './api'
import { ElMessage } from 'element-plus'
const props = defineProps({
  url: { type: String, default: '' },
  field: { type: String, required: true },
  list: { type: Array, default: () => ([]) },
  tableRef: { type: Object, default: () => ({}) },
  onlyone: Boolean,
})
const load = ref(false)
const visible = ref(false)
let loadings = reactive({})

const emit = defineEmits(['close', 'success'])

const form = ref({})
const selection = ref()
const success = ref(false)
const item = ref()

const handleAudit = (val) => {
  const selection = props.tableRef.$table.getCheckboxRecords()
  open(selection, props.list?.find(d => d.value === val))
}

// 打开弹窗
const open = async (checkeds, option) => {
  if (checkeds && !checkeds.length) {
    ElMessage.warning('请先选择行!')
    return
  }
  if ([...new Set(checkeds.map(d => d.audit_status))].length > 1) {
    ElMessage.warning('只能选择相同审核状态的数据!')
    return
  }
  selection.value = checkeds
  success.value = false
  item.value = option || { label: 'title', value: 1 }
  if (!load.value) {
    load.value = true
    await 1
  }
  form.value = {}
  loadings = reactive({})
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
  success.value && emit('success')
}
// 提交
const submit = () => {
  loadings.submit = true
  const { url, field } = props
  if (props.onlyone) {
    form.value[field] = selection.value[0][field]
  } else {
    form.value[field + 's'] = selection.value.map(d => d[field])
  }
  form.value.audit_status = item.value.value
  api.submit(url, form.value).then(res => {
    ElMessage.success('操作成功')
    success.value = true
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>